package com.longjianghuasheng.logistics.business.login

import android.os.Bundle
import android.view.View
import androidx.lifecycle.viewModelScope
import com.longjianghuasheng.logistics.BR
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseFragment
import com.longjianghuasheng.logistics.data.bindadapter.CustomBindAdapter.afterTextChanged
import com.longjianghuasheng.logistics.databinding.FragmentVerificationCodeLoginBinding
import com.longjianghuasheng.logistics.eventViewModel
import com.longjianghuasheng.logistics.ext.changeBtn
import com.longjianghuasheng.logistics.ext.checkPhone
import com.longjianghuasheng.logistics.util.CacheUtil
import com.longjianghuasheng.logistics.util.StatusBarUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.hgj.jetpackmvvm.ext.nav

class LoginFragment :
    BaseFragment<LoginViewModel, FragmentVerificationCodeLoginBinding>() {

    override fun initViewModelId(): Int {
        return BR.loginViewModel
    }


    override fun layoutId(): Int = R.layout.fragment_verification_code_login


    override fun initView(savedInstanceState: Bundle?) {

        mDatabind.initListener()
    }

    private fun FragmentVerificationCodeLoginBinding.initListener() {

        tvSwitchLogin.setOnClickListener {
            if (mViewModel.loginType) {
                mViewModel.loginType = false
                btGetCode.visibility = View.VISIBLE
                edtCode.visibility = View.VISIBLE
                edtPassword.visibility = View.GONE
                tvSwitchLogin.text = "切换为密码登录"
            } else {
                mViewModel.loginType = true
                btGetCode.visibility = View.GONE
                edtCode.visibility = View.GONE
                edtPassword.visibility = View.VISIBLE
                tvSwitchLogin.text = "切换为验证码登录"
            }
            edtCode.setText("")
            edtPassword.setText("")
        }

        edtPhone.afterTextChanged {
            btnLogin.changeBtn { checkPhone(it, false) && edtCode.text.toString().isNotBlank() }
        }
        edtCode.afterTextChanged {
            btnLogin.changeBtn {
                checkPhone(
                    edtPhone.text.toString(),
                    false
                ) && it.isNotBlank()
            }
        }

        edtPassword.afterTextChanged {
            btnLogin.changeBtn { !edtPassword.text.isNullOrBlank() }
        }

        tvRegister.setOnClickListener { navigate(R.id.registerFragment) }
        tvForgetThePassword.setOnClickListener { navigate(R.id.forgetThePasswordFragment) }
        btnLogin.setOnClickListener {
            mViewModel.login()
        }


    }


    override fun createObserver() {
        super.createObserver()
        mViewModel.result.observe(this) {
            CacheUtil.isLogin = true
            CacheUtil.token = it.token ?: ""
            CacheUtil.isDriver = it.type == 2
            eventViewModel.loginEvent.value = 0

            mViewModel.viewModelScope.launch {


                delay(300)
                withContext(Dispatchers.Main) {

                    nav().popBackStack(R.id.main2Fragment, false)
                }


            }


//            navigate(R.id.main2Fragment) {
//
//                setPopUpTo(R.id.main2Fragment, false)
//                this
//            }
        }
    }

    override fun initData() {
        super.initData()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
    }


    override fun onResume() {
        super.onResume()
        StatusBarUtil.setLightMode(requireActivity())
    }

}